/**
 * com.dingjian.permission.controller.JobPermissionListController.java
 */
package com.dingjian.permission.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.dingjian.base.CoreEntity;
import com.dingjian.base.service.BaseService;
import com.dingjian.base.util.StringUtils;
import com.dingjian.basedata.org.model.Job;
import com.dingjian.framework.base.controller.ListController;
import com.dingjian.permission.model.JobPermission;
import com.dingjian.permission.model.PermissionItem;
import com.dingjian.permission.service.JobPermissionService;

/**
 * @author FengMy
 *
 * @since 2012-10-7
 */
@Controller
@RequestMapping(value="permission/jobPermission/*")
public class JobPermissionListController extends ListController {
	@Autowired
	private JobPermissionService jobPermissionService;
	@Override
	protected CoreEntity createNewEntity() {
		return null;
	}

	@Override
	protected String getListView() {
		return "permission/jobPermissionList";
	}

	@Override
	protected String getEditView() {
		return null;
	}

	@Override
	protected String getListMapper() {
		return "com.dingjian.permission.dao.JobPermissionDao.select";
	}

	@Override
	protected BaseService<JobPermission> getService() {
		return jobPermissionService;
	}
	
	@Override
	protected Map<String, Object> getListDataParam() {
		Map<String, Object> param = super.getListDataParam();
		if(param.get("job")==null){
			param.put("job", "nodata");
		}
		return param;
	}
	
	@RequestMapping(value="saveJobPermission")
	public void saveJobPermission(HttpServletResponse response){
		String pids = getString("permissions");
		String jobid = getString("job");
		if(!StringUtils.isEmpty(jobid) && !StringUtils.isEmpty(pids)){
			String[] pidArr = pids.split(";");
			List<JobPermission> jps = new ArrayList<JobPermission>();
			Job job = new Job();
			job.setId(jobid);
			for(String pid : pidArr){
				PermissionItem pi = new PermissionItem();
				pi.setId(pid);
				JobPermission jp = new JobPermission();
				jp.setJob(job);
				jp.setPermissionItem(pi);
				jps.add(jp);
			}
			jobPermissionService.saveJobPermission(jps, new ArrayList<String>());
		}
		getOutputMsg().put("STATE", "SUCCESS");
		getOutputMsg().put("MSG", "保存成功");
		outPrint(response, JSONObject.fromObject(getOutputMsg()).toString());
	}
	
}
